package com.hackerrank.challenges.findasubstring;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();
		in.nextLine();
		Map<String, Integer> c = new HashMap<String, Integer>();
		for(int n = 0; n < N; n++) {
			String w[] = in.nextLine().split("\\W+");
			for(String word : w) {
				Integer cv = c.get(word);
				if(cv == null)
					cv = 0;
				c.put(word, cv + 1);
			}
		}
		int T = in.nextInt();
		in.nextLine();
		for(int t = 0; t < T; t++) {
			String u = in.nextLine();
			int uc = 0;
			for(String s : c.keySet())
				if(s.matches("\\w+" + u + "\\w+"))
					uc += c.get(s);
			System.out.println(uc);
		}
	}
}
